package com.lxw.manage_cms;

import com.alibaba.fastjson.JSON;
import com.lxw.framework.domain.cms.CmsPage;
import com.lxw.manage_cms.dao.CmsPageDao;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.*;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Optional;

@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class MongoDbCRUD {

    @Autowired
    CmsPageDao cmsPageDao;

    @Test
    public void testAdd(){
        //测试添加数据
        CmsPage cmsPage = new CmsPage();
        cmsPage.setPageName("测试页面添加");
        CmsPage insert = cmsPageDao.insert(cmsPage);
        log.info("添加数据{}",insert);
    }

    @Test
    public void testSave(){
        //测试添加数据
        CmsPage cmsPage = new CmsPage();
        cmsPage.setPageName("测试页面添加");
        CmsPage insert = cmsPageDao.save(cmsPage);
        log.info("添加数据{}",insert);
    }

    @Test
    public void testUpdate(){
        //测试跟新数据
        CmsPage cmsPage = new CmsPage();
        cmsPage.setPageId("612611c013ab772410e13a98");
        cmsPage.setPageName("测试页面更新");
        CmsPage insert = cmsPageDao.save(cmsPage);
        log.info("更新返回数据{}",insert);
    }

    @Test
    public void testFind(){
        //测试查询数据全部
        List<CmsPage> all = cmsPageDao.findAll();

        log.info("查询全部返回数据{}，条数{}",all,all.size());

        //根据ID
        Optional<CmsPage> byId = cmsPageDao.findById("612611c013ab772410e13a9");
        if (byId.isPresent()){
            CmsPage cmsPage = byId.get();
            log.info(("获取到的数据为:{}"), cmsPage);
        }else{
            log.info("查询为空");
        }

        //分页查询
        PageRequest of = PageRequest.of(1,2);  //页码page从0开始
        Page<CmsPage> all1 = cmsPageDao.findAll(of);
        log.info(("当前查询{}---{}"),all1.getContent(),all1.getTotalPages());

        //条件查询
        CmsPage cmsPage = new CmsPage();
        cmsPage.setPageName("测试页面添加");
        Example<CmsPage> example = Example.of(cmsPage);
        List<CmsPage> all2 = cmsPageDao.findAll(example);

        log.info("查询全部返回数据{}，条数{}",all2,all2.size());

        //分页条件查询

        //自定义查询
        CmsPage byPageNameAndTemplateId = cmsPageDao.findByPageNameAndTemplateId("index.html", "5a962b52b00ffc514038faf7");
        log.info("查询全部返回数据{}",byPageNameAndTemplateId);
    }

    @Test
    public void testDel(){
        //测试添加数据
        //cmsPageDao.deleteById("612611c013ab772410e13a98");
        CmsPage cmsPage = new CmsPage();
       // cmsPage.setTemplateId("5a7be667d019f14d90a1fb1c");
        cmsPage.setPageAliase("课程");
        //模糊查询
        ExampleMatcher.GenericPropertyMatcher contains = ExampleMatcher.GenericPropertyMatchers.contains();  //like  %value%
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("pageAliase",contains);
        Example<CmsPage> example = Example.of(cmsPage,exampleMatcher);
        List<CmsPage> all2 = cmsPageDao.findAll(example);
        log.info("查询全部返回数据{}",all2);


        PageRequest of = PageRequest.of(1,2);  //页码page从0开始
        Page<CmsPage> all1 = cmsPageDao.findAll(example,of);
        log.info("查询全部返回数据{}", JSON.toJSONString(all1));
    }
}
